# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
### Reducing Prejudice and Support for Religious 
### Nationalism Through Conversations on WhatsApp 

### Author: Rajeshwari Majumdar
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# File:     results_religiousnatlm.R
# Purpose:  analyze religious nationalism outcomes (statement approval); 
#           produce Tables 3, C.5, D.8, D.9, and D.12
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Load data and custom functions 
rm(list=ls())
load("data/Completes_Merged_Anonymized.RData")
source("code/02_setup_functions.R")

# %%%%%%%%%%%%%%%%%%%%%%%%%%
# Main text ----
# %%%%%%%%%%%%%%%%%%%%%%%%%%

# Table 3: Effect of treatment on approval for religious nationalist statements
## First panel: Hindu nation (last day)
fn_reg_main(depvar = "twt_HinduNation_q1", data = d)
## Second panel: Hijab ban (last day)
fn_reg_main(depvar = "twt_Hijab_q1", data = d)
## Third panel: Violence (3 weeks later)
fn_reg_main(depvar = "twt_HinduWeap_q1", data = d)
## Fourth panel: Terrorism (3 weeks later)
fn_reg_main(depvar = "twt_Terrorism_q1", data = d)

# %%%%%%%%%%%%%%%%%%%%%%%%%%
# Appendix ----
# %%%%%%%%%%%%%%%%%%%%%%%%%%

# Table C.5: Effect of treatment on social media actions in response to religious nationalist statements
fn_reg_main(depvar = "twt_HinduNation_action.sum", data = d)
fn_reg_main(depvar = "twt_Hijab_action.sum", data = d)
fn_reg_main(depvar = "twt_HinduWeap_action.sum", data = d)
fn_reg_main(depvar = "twt_Terrorism_action.sum", data = d)


# Tables D.8 & D.9: Effect of treatment on religious nationalism outcomes with FDR-adjusted p-values  
RNvars = c("twt_HinduNation_q1","twt_Hijab_q1","twt_HinduWeap_q1","twt_Terrorism_q1")
pvalues.notadj = numeric()
pvalues.notadj_non = numeric()
pvalues.notadj_gen = numeric()
pvalues.notadj_igr = numeric()
for (i in 1:length(RNvars)){
  mod = lm(as.formula(paste(RNvars[i], " ~ condition_pair + prejudice_binary")), data = d %>% filter(religion=="Hindu"))
  pvalues.notadj[i] = summary(mod)$coefficients[2,4]
  mod = lm(as.formula(paste(RNvars[i], " ~ condition_pair + prejudice_binary")),  data = d %>% filter(religion=="Hindu" & condition_topic=="non"))
  pvalues.notadj_non[i] = summary(mod)$coefficients[2,4]
  mod = lm(as.formula(paste(RNvars[i], " ~ condition_pair + prejudice_binary")),  data = d %>% filter(religion=="Hindu" & condition_topic=="gen"))
  pvalues.notadj_gen[i] = summary(mod)$coefficients[2,4]
  mod = lm(as.formula(paste(RNvars[i], " ~ condition_pair + prejudice_binary")),  data = d %>% filter(religion=="Hindu" & condition_topic=="igr"))
  pvalues.notadj_igr[i] = summary(mod)$coefficients[2,4]
}

## Table D.8 (overall effects)
pvalues.fdradj = p.adjust(pvalues.notadj, "BH")
data.frame(Statement = RNvars, Non.adjusted = sprintf("%.5f", pvalues.notadj), FDR.adjusted = sprintf("%.5f", pvalues.fdradj))

## Table D.9 (by topic) 
pvalues.notadj = c(pvalues.notadj_non, pvalues.notadj_gen, pvalues.notadj_igr)
pvalues.fdradj = p.adjust(pvalues.notadj, "BH")
data.frame(Statement = RNvars, Non.adjusted = sprintf("%.5f", pvalues.notadj), FDR.adjusted = sprintf("%.5f", pvalues.fdradj))


# Table D.12 Effect of treatment on approval for religious nationalist statements (six-level treatment variable)
fn_reg_sixer(depvars = c("twt_HinduNation_q1","twt_Hijab_q1","twt_HinduWeap_q1","twt_Terrorism_q1"),
               depvars_names = c("Hindu nation","Hijab ban","Violence","Terrorism"),
               data = d)
